'''
文件的读取
'''

def read1():
    # 打开文件句柄file
    file = open("file.txt");
    # file = open("file.txt","r"); 默认只读方式打开
    # 调用文件的 file read、write
    content = file.read()  # 默认从开始读取到末尾
    print(content)
    # 关闭资源file
    if file:
        file.close()

def read2():
    # 打开文件句柄file
    file = open("file.txt");
    # 调用文件的 file read、write
    line = file.readline()  # 读取第一行
    # print(line)  #print默认结尾会追加一个\r\n 回车换行符
    print(line,end="")
    line = file.readline()  #从第二行
    print(line,end="")

    # 关闭资源file
    if file:
        file.close()


def read3():
    # 打开文件句柄file
    file = open("file.txt");
    for line in file:
        print(line,end="")
    # 关闭资源file
    if file:
        file.close()


def read4():
    # with自动关闭资源
    with open("file.txt") as file:
        for line in file:
            print(line,end="")


def read5():
    # 设置文件的编码 utf-8
    with open("file3.txt",'r',encoding="utf-8") as file:
        for line in file:
            print(line,end="")


def write1():
    '''
        mode
            r  read读文件
            w  write写文件
            r+  read读  可写
            w+  write写，读
            rb  read读文件(针对二进制文件)
            wb  write写文件 (针对二进制文件)
            a  append： 追加
    '''
    # 打开文件句柄file
    file = open("file2.txt", mode="w");
    # 调用文件的 file read、write
    file.write("hello io")
    # 关闭资源file
    file.close()


if __name__ == '__main__':
    read5()